<?
require_once('functions.php');

function GetProducts()
{
		
		$con = GetConnection();
		$db = mysql_select_db("n02447784_db",$con);
        $sql = "SELECT * from Products";
		return $results = mysql_query($sql,$con);
		
}
function BlankProduct()
{
    return array('id'=>null, 'created_at'=>null,'updated_at'=>null,'product_name'=>null,'price'=>null,'quantity'=> null);
}
function GetProduct($id)
{
        $con = GetConnection();
		$db = mysql_select_db("n02447784_db",$con);
        $sql = "SELECT * FROM Products WHERE id=$id";
        return $results = mysql_query($sql, $con);	
}  
function DeleteProduct($id)
{
        $con = GetConnection();
		$db = mysql_select_db("n02447784_db",$con);
        mysql_query("DELETE FROM Products WHERE id=$id");
        return $results = mysql_query($sql, $con);	
}   
function SaveProduct(&$row)
{
    $con = GetConnection();
	$db = mysql_select_db("n02447784_db",$con);
    if(empty($row['id']))
    { 
        $sql =  "INSERT INTO Products(product_name, price, quantity) 
               VALUES ('$row[product_name]', '$row[price]', '$row[quantity]') ";
        }else{
        $sql =  "UPDATE Products 
               SET product_name = '$row[product_name]', price = '$row[price]', quantity = '$row[quantity]' 
               WHERE id=$row[id] ";
    }
    	$return = mysql_query($sql,$con);
				
				mysql_close($con);
				return $return;
}
function ValidateProduct($row)
{
    $errors = array();
	if(empty($row['product_name'])) $errors['product_name'] = 'Product Name is required';
    if(empty($row['price'])) $errors['price'] = 'Price is required';
    if(empty($row['quantity'])) $errors['quantity'] = 'Quantity is required';  
    return count($errors) == 0 ? true : $errors;
}
?> 